"""
活动
"""
from fastapi import APIRouter, Depends, Response
from sqlalchemy.orm import Session
from typing import List
from fastapi import Query

from app.dependencies import get_db
from schemas import (
    Activity,
    ActivityCreate,
    ActivityAdjust,
    ActivityDelete,
    RespList
)
from sql_app.crud.activity import crud_activity
from utils import resp_200, await_task, resp_detail_200

from . import logger


router = APIRouter(
    tags=["activity"],
    responses={404: {"description": "Not Found"}}
)


@router.get("/get_list")
async def _list(
        accesstoken: str = Query(..., example="TOKEN3590"),
        pageidx: int = Query(..., example = 1),
        pagesize: int = Query(..., example =20), keyword: str = None,
        db: Session = Depends(get_db)) -> Response:
    total = await await_task(
        crud_activity.get_count, db
    )
    orm_data = await await_task(
        crud_activity.get_a_batch, db, (pageidx-1) * pagesize, pagesize, keyword)
    data = [Activity.from_orm(i) for i in orm_data]
    crud_activity.hook(db, data)
    return resp_200(total=total, data=data)


@router.get("/get_info")
async def _info(accesstoken: str, id: int, db: Session = Depends(get_db)):
    data = await await_task(
        crud_activity.get_detail, db, id)

    return resp_detail_200(data=Activity.from_orm(data))


@router.post("/add")
async def _add(activity: ActivityCreate, db: Session = Depends(get_db)):
    await await_task(crud_activity.create, db, activity)
    return resp_200()


@router.post("/adjust")
async def _adjust(activity: ActivityAdjust, db: Session = Depends(get_db)):
    await await_task(crud_activity.adjust, db, activity)
    return resp_200()


@router.post("/delete")
async def _delete(activity: ActivityDelete, db: Session = Depends(get_db)):
    await await_task(crud_activity.delete, db, activity)
    return resp_200()
